from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
from models import *
from Puer.constant import constant
import json
import datetime
import traceback
from django.db.models import F
from django.db import transaction


# 添加购物车
def add_cart(request):
    get_product_id = int(request.GET.get('productId'))
    get_user_id = request.GET.get('userId')
    # get_size = request.GET.get('size')
    # get_color = request.GET.get('color')
    get_product_number = int(request.GET.get('number'))
    product = Product.objects.get(productid=get_product_id)
    # 库存
    # stock = product.stock
    #
    # if stock < get_product_number:
    #     return HttpResponse(json.dumps(["failure", "understock"]), content_type=constant.CONTENT_TYPE)
    try:
        # product_standard = Productstandard.objects.filter(
        #     name=get_size,
        #     color=get_color)
        user_shoppingcart = Shoppingcart.objects.filter(userid=get_user_id, productid=get_product_id)
        if user_shoppingcart:
            Shoppingcart.objects.filter(userid=get_user_id, productid=get_product_id).update(
                number=F("number") + get_product_number)
        else:
            Shoppingcart.objects.create(
                userid=get_user_id,
                productid=get_product_id,
                date=datetime.datetime.now(),
                # standardid=product_standard[0].standardid,
                number=get_product_number
            )
        # Productstandard.objects.create(
        #     productid=get_product_id,
        #     name=json.loads(get_size)[1],
        #     color=json.loads(get_size)[0],
        #     stock=get_product_number
        #
        # )
    except:
        error_msg = "%s" % traceback.format_exc()
        return HttpResponse(json.dumps(["failure", error_msg]), content_type=constant.CONTENT_TYPE)
    return HttpResponse(json.dumps('success'), content_type=constant.CONTENT_TYPE)


# 衣柜
def show_cart(request):
    try:
        get_user_id = request.GET.get('userId')
        carts = Shoppingcart.objects.filter(userid=get_user_id)
        print(carts)
        data = []
        for cart in carts:
            product_id = cart.productid
            print(product_id)
            # standard_id = cart.standardid
            # product_standard = Productstandard.objects.get(standardid=standard_id)
            product = Product.objects.filter(productid=product_id, status=constant.PRODUCT_STATE)
            if product:
                dic = {
                    # 商品id
                    'product_id': product[0].productid,
                    # 商品名称
                    'product_name': product[0].name,
                    # # 颜色
                    # 'color': product_standard.color,
                    # # 尺码
                    # 'size': product_standard.name,
                    # 缩略图
                    'product_thu': "%s%s" % (constant.FILE_ADDRESS, product[0].thumbnail),

                    # 价格
                    'price': "%.2f" % float(product[0].price),
                    # 衣柜id
                    'cart_id': cart.cartid,
                    'number': cart.number

                }
                data.append(dic)
        # 衣柜数量
    except:
        error_msg = "%s" % traceback.format_exc()
        return HttpResponse(json.dumps(["failure", error_msg]), content_type=constant.CONTENT_TYPE)
    return HttpResponse(json.dumps(data), content_type=constant.CONTENT_TYPE)


# 衣柜删除
def delete_cart(request):
    try:
        get_user_id = request.GET.get('userId')
        get_cart_ids = json.loads(request.GET.get('cartId'))

        if not get_cart_ids:
            raise Exception("cart id list is None!")

        for cart_id in get_cart_ids:
            Shoppingcart.objects.filter(cartid=int(cart_id), userid=get_user_id).delete()
    except:
        error_msg = "%s" % traceback.format_exc()
        return HttpResponse(json.dumps(["failure", error_msg]), content_type=constant.CONTENT_TYPE)
    return HttpResponse(json.dumps('success'), content_type=constant.CONTENT_TYPE)
